package com.cskaoyan.service.wxservice.impl;

import com.cskaoyan.bean.chaosbean.home.HomeIndexVo;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.wxservice.HomeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * @author dengqwe
 * @datetime 2023/1/1 18:04
 * @description: TODO:
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    MarketAdMapper adMapper;
    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketCategoryMapper categoryMapper;
    @Autowired
    MarketCouponMapper couponMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketTopicMapper topicMapper;
    @Autowired
    MarketSystemMapper systemMapper;

    @Override
    public HomeIndexVo index() {
        MarketSystemExample systemExample = new MarketSystemExample();
        systemExample.createCriteria().andKeyNameLike("%market_wx%");
        List<MarketSystem> systemList = systemMapper.selectByExample(systemExample);
        HashMap<String, Integer> map = new HashMap<>();
        for (MarketSystem system : systemList) {
            String keyName = system.getKeyName();
            String value = system.getKeyValue();
            if ("true".equals(value)||"false".equals(value)){
                continue;
            }
            Integer limt = Integer.parseInt(value);
            map.put(keyName,limt);
        }
        //PageHelper.startPage(1, map.get("market_wx_index_brand"));
        //创建一个homeindex的接受对象，为banner赋值
        HomeIndexVo data = new HomeIndexVo();
        //查询出来MarketAd的值
        MarketAdExample example = new MarketAdExample();
        MarketAdExample.Criteria criteria4 = example.createCriteria();
        criteria4.andDeletedEqualTo(false);
        List<MarketAd> marketAds = adMapper.selectByExample(example);
        data.setBanner(marketAds);

        //为brandList赋值
        MarketBrandExample example1 = new MarketBrandExample();
        example1.createCriteria().andDeletedEqualTo(false);
        List<MarketBrand> marketBrands = brandMapper.selectByExample(example1);
        List<MarketBrand> marketBrands1 = new ArrayList<>();
        Random random = new Random();
        //从数据库中拿到要显示的数量
        Integer num = map.get("market_wx_index_brand");

        if (marketBrands.size() >=num ) {
            for (int i = 0; i < num; i++) {
                marketBrands1.add(marketBrands.remove(random.nextInt(marketBrands.size())));
            }
        } else {
            marketBrands1 = marketBrands;
        }
        data.setBrandList(marketBrands1);

        //为categoryList赋值
        MarketCategoryExample example2 = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria5 = example2.createCriteria();
        criteria5.andDeletedEqualTo(false).andPidEqualTo(0);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example2);
        List<MarketCategory> MarketCategory1 = new ArrayList<>();
        //只显示6个
        /*Integer num1 = map.get("market_wx_catlog_list");
        if (marketCategories.size() >= num1) {
            for (int i = 0; i < num1; i++) {
                MarketCategory1.add(marketCategories.remove(random.nextInt(marketCategories.size())));
            }
        } else {
            MarketCategory1=marketCategories;
        }*/
        data.setChannel(marketCategories);

        //为couponList赋值
        MarketCouponExample example4 = new MarketCouponExample();

        MarketCouponExample.Criteria criteria6 = example4.createCriteria();
        criteria6.andDeletedEqualTo(false);
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(example4);
        List<MarketCoupon> MarketCoupon1 = new ArrayList<>();
        //只显示3个
        Integer num2 = map.get("market_wx_catlog_list");
        if (marketCoupons.size() >= num2) {
            for (int i = 0; i < num2; i++) {
                MarketCoupon1.add(marketCoupons.remove(random.nextInt(marketCoupons.size())));
            }
        } else {
            MarketCoupon1=marketCoupons;
        }
        data.setCouponList(MarketCoupon1);

        //为FloorGoodsList赋值，里面嵌套了一个goodlist
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria3 = categoryExample.createCriteria();
        criteria3.andDeletedEqualTo(false);
        criteria3.andPidEqualTo(0);
        //第一层的list
        ArrayList<HomeIndexVo.FloorGoodsListDTO> firstList = new ArrayList<>();

        PageHelper.startPage(1, map.get("market_wx_catlog_list"));
        List<MarketCategory> levelOneCategories = categoryMapper.selectByExample(categoryExample);

        for (MarketCategory levelOneCategory : levelOneCategories) {
            PageHelper.startPage(1, map.get("market_wx_catlog_list"));
            List<MarketGoods> goodsList = goodsMapper.selectGoodsByLevelOneCategoryId(levelOneCategory.getId());
            //List<MarketGoods> goodsList2 = goodsMapper.selectGoodsByLevelOneCategoryId2(levelOneCategory.getId());
            HomeIndexVo.FloorGoodsListDTO floorGoodsListDTO = new HomeIndexVo.FloorGoodsListDTO();
            PageInfo<MarketGoods> pageInfo = new PageInfo<>(goodsList);
            long total = pageInfo.getTotal();
            floorGoodsListDTO.setGoodsList(goodsList);
            floorGoodsListDTO.setId(levelOneCategory.getId());
            floorGoodsListDTO.setName(levelOneCategory.getName());
            firstList.add(floorGoodsListDTO);

        }

        data.setFloorGoodsList(firstList);


        //MarketGoods赋值
        MarketGoodsExample example3 = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example3.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIsNewEqualTo(true);
        List<MarketGoods> goodsList = goodsMapper.selectByExample(example3);
        List<MarketGoods> goodsList1 = new ArrayList<>();
        //只显示6个
        Integer num3 = map.get("market_wx_catlog_goods");
        if (goodsList.size() >= num3) {
            for (int i = 0; i < num3; i++) {
                goodsList1.add(goodsList.remove(random.nextInt(goodsList.size())));
            }
        } else {
            goodsList1 = goodsList;
        }
        data.setNewGoodList(goodsList1);

        //为hotGoodsList赋值
        MarketGoodsExample example5 = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria1 = example5.createCriteria();
        criteria1.andDeletedEqualTo(false);
        criteria1.andIsHotEqualTo(true);
        List<MarketGoods> hostGoodsList = goodsMapper.selectByExample(example5);
        List<MarketGoods> hostGoodsList2 = new ArrayList<>();
        //只显示4个
        Integer num4 = map.get("market_wx_index_hot");
        if (marketBrands.size() >= num4) {
            for (int i = 0; i < num4; i++) {
                hostGoodsList2.add(hostGoodsList.remove(random.nextInt(hostGoodsList.size())));
            }
        } else {
            hostGoodsList2 = hostGoodsList;
        }
        data.setHotGoodsList(hostGoodsList2);

        //为NewGoodList赋值,这里复用了example;
        MarketGoodsExample.Criteria criteria2 = example5.createCriteria();
        criteria2.andDeletedEqualTo(false);
        criteria2.andIsNewEqualTo(true);
        List<MarketGoods> goodsList2 = goodsMapper.selectByExample(example5);
        List<MarketGoods> goodsList3 = new ArrayList<>();
        //只显示4个
        Integer num5 = map.get("market_wx_index_new");
        if (goodsList2.size() >= num5) {
            for (int i = 0; i < num5; i++) {
                goodsList3.add(goodsList2.remove(random.nextInt(goodsList2.size())));
            }
        } else {
            goodsList3 = goodsList2;
        }
        data.setNewGoodList(goodsList3);
        //为topicList赋值
        //
        MarketTopicExample topicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria7 = topicExample.createCriteria();
        criteria7.andDeletedEqualTo(false);
        List<MarketTopic> topicList = topicMapper.selectByExample(topicExample);
        List<MarketTopic> topicList1 = new ArrayList<>();
        //只显示6个
        Integer num6 = map.get("market_wx_index_topic");
        if (topicList.size() >= 4) {
            for (int i = 0; i <num6 ; i++) {
                topicList1.add(topicList.remove(random.nextInt(topicList.size())));
            }
        } else {
            topicList1 = topicList;
        }
        data.setTopicList(topicList1);


        return data;
    }
}
